package com.hougong.im;

import com.alibaba.fastjson.JSON;
import com.hougong.im.UDP.UDPServer;
import com.hougong.im.annotation.IMAction;
import com.hougong.im.transport.BaseFrame;
import com.hougong.im.transport.NettyConfig;
import com.hougong.im.transport.NettyConfigImpl;
import com.hougong.im.utils.SpringContextUtils;
import io.netty.channel.Channel;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.Map;

@SpringBootApplication
public class ImApplication  implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(ImApplication.class, args);
    }
    @Override
    public void run(String... args) throws Exception {
        initRequestController();

        new Thread(new Runnable() {
            @Override
            public void run() {
                //springboot 容器初始化后 启动netty服务
                NettyConfig nettyConfig=new NettyConfigImpl();
                nettyConfig.setParentGroup(1);
                nettyConfig.setChildGroup();
                nettyConfig.setChannel(NioServerSocketChannel.class);
                nettyConfig.setHandler();
                nettyConfig.bind(20000);
            }
        }).start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                UDPServer udpServer=new UDPServer();
                udpServer.run(20001);
            }
        }).start();
    }

    private void initRequestController(){
        Map<String, Object> beansWithAnnotation = SpringContextUtils.applicationContext.getBeansWithAnnotation(Service.class);
        for(Map.Entry<String,Object> entry:beansWithAnnotation.entrySet()){
            Object bean = entry.getValue();
            Method[] methods = bean.getClass().getMethods();
            for(Method method:methods){
                if(method.isAnnotationPresent(IMAction.class)){
                    Class<?>[] parameterTypes = method.getParameterTypes();
                    if(parameterTypes.length!=2){
                        throw new RuntimeException(entry.getKey()+"#"+method.getName()+" 参数不是2 个");
                    }
                    if(parameterTypes[0]!=BaseFrame.class||parameterTypes[1]!=Channel.class){
                        throw new RuntimeException(entry.getKey()+"#"+method.getName()+" 参数列表类型不对");
                    }
                    IMAction annotation = method.getAnnotation(IMAction.class);
                    short value = annotation.value();
                    Map<Short, String> requesytHandlerMap = Dispather.getRequestHandlerMap();
                    requesytHandlerMap.put(value,entry.getKey()+"#"+method.getName());
                }
            }
        }
        System.out.println(JSON.toJSONString(Dispather.getRequestHandlerMap()));
    }

}
